DeleteLeaveRequestCommandHandler.execute   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 17
c 0
b 0
f 0
rs 9.7
cc 3
1
import { Inject } from '@nestjs/common';
2
import { CommandHandler } from '@nestjs/cqrs';
3
import { DeleteLeaveRequestCommand } from './DeleteLeaveRequestCommand';
4
import { ILeaveRequestRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRequestRepository';
5
import { LeaveRequestNotFoundException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestNotFoundException';
6
import { DoesLeaveRequestBelongToUser } from 'src/Domain/HumanResource/Leave/Specification/DoesLeaveRequestBelongToUser';
7
import { LeaveRequestCantBeRemovedException } from 'src/Domain/HumanResource/Leave/Exception/LeaveRequestCantBeRemovedException';
8
9
@CommandHandler(DeleteLeaveRequestCommand)
10
export class DeleteLeaveRequestCommandHandler {
11
  constructor(
12
    @Inject('ILeaveRequestRepository')
13
    private readonly leaveRequestRepository: ILeaveRequestRepository,
14
    private readonly doesLeaveRequestBelongToUser: DoesLeaveRequestBelongToUser
15
  ) {}
16
17
  public async execute(command: DeleteLeaveRequestCommand): Promise<void> {
18
    const { owner, id } = command;
19
20
    const leaveRequest = await this.leaveRequestRepository.findOneById(id);
21
    if (!leaveRequest) {
22
      throw new LeaveRequestNotFoundException();
23
    }
24
25
    if (
26
      false ===
27
      this.doesLeaveRequestBelongToUser.isSatisfiedBy(leaveRequest, owner)
28
    ) {
29
      throw new LeaveRequestCantBeRemovedException();
30
    }
31
32
    this.leaveRequestRepository.remove(leaveRequest);
33
  }
34
}
35